//
//  UIECGSettingsView.m
//  vhECGTools
//
//  Created by jia yu on 2021/11/22.
//

#import "UIECGSettingsView.h"
#import "UIECGPreview.h"
#import "UIECGControlTableView.h"

@interface UIECGSettingsView(){
    UIECGPreview *preview;
    UIECGControlTableView *controlView;
}

@end


@implementation UIECGSettingsView
//补充初始化缺失的值，用默认值填充
-(void)checkDefaultKey:(NSString *)mainKey withDefaultWidth:(CGFloat)width withDefaultColor:(UIColor *)color{
    const CGFloat *cmp = CGColorGetComponents(color.CGColor);
    NSString *subkey = [mainKey stringByAppendingString:Color_R_Key];
    if (nil == [controlView.lineDictionary valueForKey:subkey]){
        [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[0]] forKey:subkey];
    }
    subkey = [mainKey stringByAppendingString:Color_G_Key];
    if (nil == [controlView.lineDictionary valueForKey:subkey]){
        [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[1]] forKey:subkey];
    }
    subkey = [mainKey stringByAppendingString:Color_B_Key];
    if (nil == [controlView.lineDictionary valueForKey:subkey]){
        [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[2]] forKey:subkey];
    }
    subkey = [mainKey stringByAppendingString:Color_A_Key];
    if (nil == [controlView.lineDictionary valueForKey:subkey]){
        [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[3]] forKey:subkey];
    }
    subkey = [mainKey stringByAppendingString:Width_Key];
    if (nil == [controlView.lineDictionary valueForKey:subkey]){
        [controlView.lineDictionary setValue:[NSNumber numberWithFloat:width] forKey:subkey];
    }
}

//设置了新的值
-(void)setWidth:(CGFloat)width Color:(UIColor *)color forKey:(NSString *)mainKey{
    const CGFloat *cmp = CGColorGetComponents(color.CGColor);
    NSString *subkey = [mainKey stringByAppendingString:Color_R_Key];
    [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[0]] forKey:subkey];
    subkey = [mainKey stringByAppendingString:Color_G_Key];
    [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[1]] forKey:subkey];
    subkey = [mainKey stringByAppendingString:Color_B_Key];
    [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[2]] forKey:subkey];
    subkey = [mainKey stringByAppendingString:Color_A_Key];
    [controlView.lineDictionary setValue:[NSNumber numberWithFloat:cmp[3]] forKey:subkey];
    subkey = [mainKey stringByAppendingString:Width_Key];
    [controlView.lineDictionary setValue:[NSNumber numberWithFloat:width] forKey:subkey];
    return;
}

-(NSDictionary *)currentSettings{
    return controlView.lineDictionary;
}

-(void)setDefaultDictionary:(NSDictionary * _Nullable )dict{
    if (nil == dict){
        controlView.lineDictionary = [[NSMutableDictionary alloc] init];
    }else{
        controlView.lineDictionary = [[NSMutableDictionary alloc] initWithDictionary:dict];
    }
    [self checkDefaultKey:Bg1mmLineKey withDefaultWidth:1.f withDefaultColor:[UIColor colorWithRed:0.93 green:0.93 blue:0.94 alpha:0.9]];
    [self checkDefaultKey:Bg5mmLineKey withDefaultWidth:1.2f withDefaultColor:[UIColor colorWithRed:0.72 green:0.70 blue:0.84 alpha:0.7f]];
    [self checkDefaultKey:Bg10mmLineKey withDefaultWidth:2.f withDefaultColor:[UIColor colorWithRed:0.58 green:0.72 blue:0.73 alpha:1.f]];
    [self checkDefaultKey:BgColorPixPermmKey withDefaultWidth:6.2f withDefaultColor:[UIColor whiteColor]];
    [self checkDefaultKey:ECGLineKey withDefaultWidth:1.8f withDefaultColor:[UIColor blackColor]];
    
    [preview setNeedsDisplay];
    return;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        UIECGSettingsView * __strong strongSelf = self;
        self.layer.masksToBounds = YES;
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        preview = [[UIECGPreview alloc] initWithFrame:CGRectMake(0.f, 0.f, frame.size.width, 100.f)];
        preview.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        preview.backgroundColor = [UIColor whiteColor];
        preview.settings = self;
        [self addSubview:preview];
        
        float y = CGRectGetMaxY(preview.frame);
        controlView = [[UIECGControlTableView alloc] initWithFrame:CGRectMake(0.f, y, frame.size.width, frame.size.height-y-50.f) style:UITableViewStylePlain];
        controlView.settingsView = self;
        controlView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;;
        controlView.handler = ^(NSString *key, float width, UIColor *color) {
            [strongSelf setWidth:width Color:color forKey:key];
            [strongSelf->preview setNeedsDisplay];
        };
        [self addSubview:controlView];
        
        [self setDefaultDictionary:nil];
    }
    return self;
}

-(CGFloat)widthWithKey:(NSString *)key{
    return [[controlView.lineDictionary valueForKey: [key stringByAppendingString:Width_Key]] floatValue];
}
-(UIColor *)colorWithKey:(NSString *)key{
    CGFloat r = [[controlView.lineDictionary valueForKey:[key stringByAppendingString:Color_R_Key]] floatValue];
    CGFloat g = [[controlView.lineDictionary valueForKey:[key stringByAppendingString:Color_G_Key]] floatValue];
    CGFloat b = [[controlView.lineDictionary valueForKey:[key stringByAppendingString:Color_B_Key]] floatValue];
    CGFloat a = [[controlView.lineDictionary valueForKey:[key stringByAppendingString:Color_A_Key]] floatValue];
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

-(CGFloat)ecgLineWidth{
    return [self widthWithKey:ECGLineKey];
}
-(UIColor *)ecgColor{
    return [self colorWithKey:ECGLineKey];
}

-(CGFloat)pixPermm{
    return [self widthWithKey:BgColorPixPermmKey];
}
-(UIColor *)ecgBackgroundColor{
    return [self colorWithKey:BgColorPixPermmKey];
}

-(CGFloat)bg1mmLineWidth{
    return [self widthWithKey:Bg1mmLineKey];
}
-(UIColor *)bg1mmLineColor{
    return [self colorWithKey:Bg1mmLineKey];
}

-(CGFloat)bg5mmLineWidth{
    return [self widthWithKey:Bg5mmLineKey];
}
-(UIColor *)bg5mmLineColor{
    return [self colorWithKey:Bg5mmLineKey];
}

-(CGFloat)bg10mmLineWidth{
    return [self widthWithKey:Bg10mmLineKey];
}
-(UIColor *)bg10mmLineColor{
    return [self colorWithKey:Bg10mmLineKey];
}
@end
